<?php

namespace app\admin\model;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'order_create_time_text',
        'shipping_time_text',
        'delivered_time_text',
        'shipping_type_text'
    ];
    

    
    public function getShippingTypeList()
    {
        return ['0' => __('Shipping_type 0'), '1' => __('Shipping_type 1')];
    }


    public function getOrderCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_create_time']) ? $data['order_create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getShippingTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shipping_time']) ? $data['shipping_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeliveredTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delivered_time']) ? $data['delivered_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getShippingTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['shipping_type']) ? $data['shipping_type'] : '');
        $list = $this->getShippingTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setOrderCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setShippingTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeliveredTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function goods()
    {
        return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goodssku()
    {
        return $this->belongsTo('Goodssku', 'goodssku_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function shop()
    {
        return $this->belongsTo('Shops', 'shops_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
